import praw
import sqlite3
import random 
import time
import assoc.Graph as ng
import os
#
# Boilerpipe can be downloaded: https://pypi.python.org/pypi/boilerpipe/1.2.0.0
# or #: sudo easy_install boilerpipe
#
from boilerpipe.extract import Extractor

import logging
logging.basicConfig(format='%(asctime)m %(levelname)s: %(message)s', filename='runtime.log',level=logging.INFO)

class Database:

	def __init__(self, location="db.sql"):
		self.db = sqlite3.connect(location)

	def registerUser(self, username):
		cursor = self.db.execute("SELECT id FROM agent WHERE username=?",[username])
		agentId = cursor.fetchone()
		if agentId is not None:
			return agentId[0]
		else:
			self.db.execute("INSERT INTO agent(username) VALUES(?)",[username])
			self.db.commit()
			return self.registerUser(username)
			
	# Here you can add either the url or id or whatever which
	# gives the agent knowledge what has been crawled
	def addUrl(self, submissionUrl):
		self.db.execute("INSERT INTO crawl(url) VALUES(?)",[submissionUrl])
		self.db.commit()
		
	def visitedUrl(self, submissionUrl):
		cursor = self.db.execute("SELECT COUNT(*) FROM crawl WHERE url=?",[submissionUrl])
		if cursor.fetchone()[0] > 0:
			return True
		else:
			return False

class RedditAgent:

	def __init__(self, config):
		self.postingRate = 0.3

		self.username = self.readConfig(config, "user")
		self.password = self.readConfig(config, "pass")
		self.graphsDir = self.readConfig(config, "graphsDir")
		self.backgroundLoc = self.readConfig(config, "backgroundLocation")
		if not os.path.exists(graphsDir):
			os.makedirs(graphsDir)
		self.reddit = praw.Reddit('Testing agent')
		self.db = Database()
		self.id = self.__registerUser()
		self.login()
		
	def __registerUser(self):
		return self.db.registerUser(self.username)
		
	def readConfig(self, config, field):
		f = open(config)
		lines = f.read().split("\n")
		for l in lines:
			l = l.split(":")
			if l[0] == field:
				return l[1]
		return None
		
	def getHotTopics(self):
		subreddit = self.reddit.get_subreddit('worldnews')
		return subreddit.get_hot(limit=10)
		
	def getHotComments(self, submission):
		return self.reddit.get_submission(submission_id=submission.id,comment_sort="hot").comments
		
	def getControversialComments(self, submission):
		return self.reddit.get_submission(submission_id=submission.id,comment_sort="controversial").comments

	def calculateForeground(self, text, submission, POS):
		g = ng.Graph(self.backgroundLoc, version=2.0, POS=POS)
		g.textToGraph(text)
		g.save(g.OUTPUT_FORMAT_SQLITE, filename=self.graphsDir + str(submission.id))
		return self.graphsDir + str(submission.id)
		
	def createPost(self, submission):
		extractor = Extractor(extractor='ArticleExtractor', url=submission.url)
		extractedText = extractor.getText()
		foregroundFileLocation = "newsGraphs/867c5bfd7f7bfee8d814339864392b2c.sqlite"
		#foregroundFileLocation = self.calculateForeground(extractedText, submission, POS=True)
		
		return "This is a great poem ajee"
		
	def postOrSkip(self, submission, text=None):
		value = None
		if not self.db.visitedUrl(submission.id) and "reddit.com/r/" not in submission.url:
			if random.random() <= self.postingRate:
				if text is None:
					text = self.createPost(submission)
				logging.info("Posting to submission " + str(submission.id) + " a poem: " + text)
				value = self.post(submission, text)
			else:
				logging.info("Skipping post " + str(submission.id))
			self.db.addUrl(submission.id)
		return value
		
	def login(self):
		self.reddit.login(self.username, self.password)
		
	def post(self, submission, text):
		try:
			submission.add_comment(text)
			return True
		except praw.errors.RateLimitExceeded as e:
			if e.ERROR_TYPE == "RATELIMIT":
				time.sleep(e.sleep_time+10)
				self.post(submission, text)
			else:
				logging.error(e)
				return False
		

def main():
    agent = RedditAgent("config.ini")
    for s in agent.getHotTopics():
    	print agent.postOrSkip(s)


if  __name__ == '__main__':main()
